{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys \n",
    "sys.path.append('../scripts/')\n",
    "from dynamic_programming import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import seaborn as sns               ###dp_for_pfc\n",
    "\n",
    "puddles = []  #空にする\n",
    "dp = DynamicProgramming(np.array([0.2, 0.2, math.pi/18]).T, Goal(-1.5,-1.5), puddles, 0.1, 10)  #ゴールの位置を合わせる\n",
    "counter = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "delta = 1e100\n",
    "\n",
    "while delta > 0.01: \n",
    "    delta = dp.value_iteration_sweep()\n",
    "    counter += 1\n",
    "    print(counter, delta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"policy_for_pfc.txt\", \"w\") as f: #後からpolicy.txtにコピー（あるいはシンボリックリンク）\n",
    "    for index in dp.indexes:\n",
    "        p = dp.policy[index]\n",
    "        f.write(\"{} {} {} {} {}\\n\".format(index[0], index[1], index[2], p[0], p[1]))\n",
    "        \n",
    "with open(\"value_for_pfc.txt\", \"w\") as f:#後からvalue.txtにコピー（あるいはシンボリックリンク）\n",
    "    for index in dp.indexes:\n",
    "        p = dp.value_function[index]\n",
    "        f.write(\"{} {} {} {}\\n\".format(index[0], index[1], index[2], p))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "v = dp.value_function[:, :, 18]  ###dp1valuedraw\n",
    "sns.heatmap(np.rot90(v), square=False)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = np.zeros(dp.index_nums) ###dp1policydraw\n",
    "for i in dp.indexes:\n",
    "    p[i] = sum(dp.policy[i]) #速度と角速度を足すと、1.0: 直進、2.0: 左回転、-2.0: 右回転になる\n",
    "\n",
    "sns.heatmap(np.rot90(p[:, :, 18]), square=False) #180〜190[deg]の向きのときの行動を図示\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
